package com.small.xx.ai.apidoc.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.small.xx.ai.apidoc.entity.AppService;
import com.small.xx.ai.apidoc.dto.AppServiceRequest;
import com.small.xx.ai.apidoc.dto.AppServiceResponse;
import com.small.xx.ai.apidoc.mapper.AppServiceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 应用服务管理服务类
 */
@Service
public class AppServiceService extends ServiceImpl<AppServiceMapper, AppService> {
    
    @Autowired
    private AppServiceMapper appServiceMapper;
    
    /**
     * 创建应用服务
     * 
     * @param request 应用服务请求
     * @return 应用服务响应
     */
    public AppServiceResponse createAppService(AppServiceRequest request) {
        AppService appService = new AppService();
        appService.setName(request.getName());
        appService.setDescription(request.getDescription());
        appService.setIpAddress(request.getIpAddress());
        appService.setPort(request.getPort());
        appService.setProtocol(request.getProtocol());
        appService.setContextPath(request.getContextPath());
        appService.setEnabled(request.getEnabled());
        appService.setCreateTime(LocalDateTime.now());
        appService.setUpdateTime(LocalDateTime.now());
        
        appServiceMapper.insert(appService);
        
        return convertToResponse(appService);
    }
    
    /**
     * 根据ID获取应用服务
     * 
     * @param id 应用服务ID
     * @return 应用服务响应
     */
    public AppServiceResponse getAppServiceById(Long id) {
        AppService appService = appServiceMapper.selectById(id);
        return appService != null ? convertToResponse(appService) : null;
    }
    
    /**
     * 分页获取应用服务列表
     * 
     * @param page 页码（从1开始）
     * @param size 每页大小
     * @return 应用服务响应列表
     */
    public List<AppServiceResponse> getAppServicesByPage(int page, int size) {
        Page<AppService> pageObj = new Page<>(page, size);
        Page<AppService> result = appServiceMapper.selectPage(pageObj, null);
        return result.getRecords().stream()
                .map(this::convertToResponse)
                .collect(Collectors.toList());
    }
    
    /**
     * 获取所有应用服务
     * 
     * @return 应用服务响应列表
     */
    public List<AppServiceResponse> getAllAppServices() {
        return appServiceMapper.selectList(null).stream()
                .map(this::convertToResponse)
                .collect(Collectors.toList());
    }
    
    /**
     * 获取应用服务总数
     * 
     * @return 应用服务总数
     */
    public int getAppServiceCount() {
        return appServiceMapper.selectCount(null).intValue();
    }
    
    /**
     * 更新应用服务
     * 
     * @param id 应用服务ID
     * @param request 应用服务请求
     * @return 应用服务响应
     */
    public AppServiceResponse updateAppService(Long id, AppServiceRequest request) {
        AppService appService = appServiceMapper.selectById(id);
        if (appService == null) {
            return null;
        }
        
        appService.setName(request.getName());
        appService.setDescription(request.getDescription());
        appService.setIpAddress(request.getIpAddress());
        appService.setPort(request.getPort());
        appService.setProtocol(request.getProtocol());
        appService.setContextPath(request.getContextPath());
        appService.setEnabled(request.getEnabled());
        appService.setUpdateTime(LocalDateTime.now());
        
        appServiceMapper.updateById(appService);
        
        return convertToResponse(appService);
    }
    
    /**
     * 删除应用服务
     * 
     * @param id 应用服务ID
     * @return 是否删除成功
     */
    public boolean deleteAppService(Long id) {
        return appServiceMapper.deleteById(id) > 0;
    }
    
    /**
     * 将AppService转换为AppServiceResponse
     * 
     * @param appService 应用服务实体
     * @return 应用服务响应
     */
    private AppServiceResponse convertToResponse(AppService appService) {
        AppServiceResponse response = new AppServiceResponse();
        response.setId(appService.getId());
        response.setName(appService.getName());
        response.setDescription(appService.getDescription());
        response.setIpAddress(appService.getIpAddress());
        response.setPort(appService.getPort());
        response.setProtocol(appService.getProtocol());
        response.setContextPath(appService.getContextPath());
        response.setEnabled(appService.getEnabled());
        response.setCreateTime(appService.getCreateTime());
        response.setUpdateTime(appService.getUpdateTime());
        response.setBaseUrl(appService.getBaseUrl());
        return response;
    }
}